package com.sjj.shopping.controller;

import GoodService.GoodService;
import com.alibaba.fastjson.JSON;
import com.sjj.shopping.MultipartFileToFile;
import com.sjj.shopping.service.Common;
import common.Result;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import pojo.GoodRequest;
import pojo.GoodResponse;
import pojo.User;
import service.UserService;

import java.io.*;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

/**
 * className:UserController
 * Package:com.sjj.shopping.controller
 * Description:
 *
 * @Date: 2022/8/6 21:41
 * @Auther: wssky19890629@163.com
 */



@Controller
public class UserController {
    @Reference
    UserService userService;

    @Reference
    GoodService goodService;

    @RequestMapping("/registor")
    public String registor(){
        return "registor";
    }

    @ResponseBody
    @RequestMapping("/doRegistor")
    public String doRegistor(MultipartFile img,String username,String password,String role) throws Exception {

        User user=new User();
        user.setRole(role);
        user.setPassword(password);
        user.setUsername(username);
        try {
            String idCard = Common.imgToId(img);
            user.setIdCard(idCard);
        } catch (Exception e){
            return "你这身份证传的有问题啊，怎么识别不出来";
        }

        return userService.registor(user);
    };

    @ResponseBody
    @RequestMapping("/user")
    public String user(){
        return "user";
    }



    @RequestMapping("/mylogin")
    public String mylogin(){
        return "mylogin";
    }

    @RequestMapping("/welcome")
    public String welcome(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        if("user".equals(user.getRole())){
            return "user/index";
        }
        if("admin".equals(user.getRole())){
            return "admin/index";
        }
        if("merchant".equals(user.getRole())){
            return "merchant/index";
        }
        return "registor";


    }

    @ResponseBody
    @RequestMapping("/user/changeName/{newName}")
    public String changeName(@PathVariable(name = "newName") String newName){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        Result<Object> result = userService.modifyNickname(user, newName);
        return result.toString();
    }

    @ResponseBody
    @RequestMapping("/merchant/addGood")
    public String addGood(GoodRequest goodRequest){
        User user = getUserContext();
        goodRequest.setMerchantId(user.getUserId());
        GoodResponse goodResponse = goodService.addGood(goodRequest);
        return goodResponse.isResult()+"";
    }

    @ResponseBody
    @RequestMapping("/selectAll")
    public String selectAll(){
        System.out.println(1111);
        List<GoodResponse> goodResponses = goodService.selectAll();
        return JSON.toJSONString(goodResponses);
    }

    public User getUserContext(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        return user;
    }
}
